// AWTCheckboxPeer.cpp : implementation file
//

#include "stdafx.h"
#include "AWTCheckboxPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// AWTCheckboxPeer

AWTCheckboxPeer::AWTCheckboxPeer(AWTComponent* comp)
{
	TRACE(_T("AWTCheckboxPeer::AWTCheckboxPeer - this: %x\n"), this);
	m_comp = comp;
	m_AWTWindow = NULL;
}

AWTCheckboxPeer::~AWTCheckboxPeer()
{
	CString s;
	GetWindowText(s);
	TRACE(_T("AWTCheckboxPeer::~AWTCheckboxPeer - %s this: %x\n"), s.GetBuffer(0), this);
	if (m_hWnd != NULL)
		DestroyWindow();
}


BEGIN_MESSAGE_MAP(AWTCheckboxPeer, CButton)
	//{{AFX_MSG_MAP(AWTCheckboxPeer)
	ON_WM_MOVE()
	ON_WM_SIZE()
	ON_WM_SHOWWINDOW()
	ON_WM_CREATE()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// AWTCheckboxPeer message handlers

void AWTCheckboxPeer::OnMove(int x, int y) 
{
	CButton::OnMove(x, y);
	
	AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_MOVED);
	m_comp->processComponentEvent(&e);
}

void AWTCheckboxPeer::OnSize(UINT nType, int cx, int cy) 
{
	CButton::OnSize(nType, cx, cy);
	
	AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_RESIZED);
	m_comp->processComponentEvent(&e);
}

void AWTCheckboxPeer::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CButton::OnShowWindow(bShow, nStatus);
	
	if (bShow)	
	{
		AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_SHOWN);
		m_comp->processComponentEvent(&e);
	}
	else
	{
		AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_HIDDEN);
		m_comp->processComponentEvent(&e);
	}
}

int AWTCheckboxPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CButton::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/	
	return 0;
}


HBRUSH AWTCheckboxPeer::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	AWTColor* fg = m_comp->getForeground();
	AWTColor* bk = m_comp->getBackground();
	CBrush* br = m_comp->getBkBrush();

	if (fg != NULL)
		pDC->SetTextColor(fg->getColorRef());
	if (bk != NULL)
		pDC->SetBkColor(bk->getColorRef());
	return (HBRUSH)*br;
}

void AWTCheckboxPeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	TRACE(_T("AWTCheckboxPeer::OnKeyDown 1 - this: %x\n"), this);
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	TRACE(_T("AWTCheckboxPeer::OnKeyDown 2 - this: %x\n"), this);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);
	if (!e.consumed())
		CButton::OnKeyDown(nChar, nRepCnt, nFlags);
	TRACE(_T("AWTCheckboxPeer::OnKeyDown 3 - this: %x\n"), this);
}

void AWTCheckboxPeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == '\t')
		return;
	
	CButton::OnChar(nChar, nRepCnt, nFlags);
}

void AWTCheckboxPeer::OnSetFocus(CWnd* pOldWnd) 
{
	CButton::OnSetFocus(pOldWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTCheckboxPeer::OnKillFocus(CWnd* pNewWnd) 
{
	CButton::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTCheckboxPeer::OnClicked() 
{
	AWTCheckbox* cb = (AWTCheckbox*)m_comp;
	AWTCheckboxGroup* g = cb->getCheckboxGroup();
	CString s = ((AWTCheckbox*)m_comp)->getLabel();
	if (g != NULL)
	{
		g->setSelectedCheckbox(cb);
		AWTItemEvent e(cb, AWTItemEvent::ITEM_STATE_CHANGED, (DWORD)&s, AWTItemEvent::SELECTED);
		cb->processItemEvent(&e);
	}
	else
	{
		if (GetCheck() == 1)
		{
			AWTItemEvent e(cb, AWTItemEvent::ITEM_STATE_CHANGED, (DWORD)&s, AWTItemEvent::SELECTED);
			cb->setState(TRUE);
			cb->processItemEvent(&e);
		}
		else
		{
			AWTItemEvent e(cb, AWTItemEvent::ITEM_STATE_CHANGED, (DWORD)&s, AWTItemEvent::DESELECTED);
			cb->setState(FALSE);
			cb->processItemEvent(&e);
		}	
	}
}

void AWTCheckboxPeer::OnLButtonDown(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 1, FALSE);
	m_comp->processMouseEvent(&e);
	CButton::OnLButtonDown(nFlags, point);
}

void AWTCheckboxPeer::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 2, FALSE);
	m_comp->processMouseEvent(&e);
	CButton::OnLButtonDblClk(nFlags, point);
}

void AWTCheckboxPeer::OnDestroy() 
{
	TRACE(_T("AWTCheckboxPeer::OnDestroy - this: %x\n"), this);
	CButton::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}
